var totalPage = 0;
var totalCnt = 0;
var equipmentInfos = [];
var chargingStationData = [];
var equipmentPowerType = [];
var protocolTypeData = [];
var equipmentInfoChart;

$(document).ready(function() {
	showLoading();
	$('title', parent.document).html($('title').text());
	setBaseUrl();
	createTableHead("equipmentInfoThead", equipmentInfoTable, search);
	scrollTableSuspend("scrollBody", "equipmentInfoTable", "equipmentInfoTable1", "equipmentInfoThead", "scrollHeight");
	selectDate("date");
	equipmentInfoChart();
	$ajaxList('protocolType', getProtocolType);
	$ajaxList('chargingStation', getChargingStation);
	$ajaxList('equipmentType', getEquipmentType);
	setTimeout(firstSearch,500);
    setTimeout(hideLoading, 700);
});
function firstSearch(){
	var passValuesNum = window.parent.document.getElementById("passValue").value;
    if(passValuesNum != ""){
    	 $("#ChargingStationID").val(passValuesNum);
    	 window.parent.document.getElementById("passValue").value = '';
    }
	search();
}
function getProtocolType(data) {
	var length = data.length;
	protocolTypeData = data;
	for (var i = 0; i < length; i++) {
		$("#protocolType").append("<option value='" + data[i].equipmentType + "'>" + data[i].equipmentNameCN + "</option>");
	}
}
function getChargingStation(data) {
	var length = data.length;
	chargingStationData = data;
	for (var i = 0; i < length; i++) {
		$("#ChargingStationID").append("<option value='" + data[i].chargingStationID + "'>" + data[i].chargingStationName + "</option>");
	}
}
function getEquipmentType(data) {
	var length = data.length;
	equipmentPowerType = data;
	for (var i = 0; i < length; i++) {
		$("#EquipmentPowerType").append("<option value='" + data[i].equipmentPowerType + "'>" + data[i].equipmentPowerTypeName + "</option>");
	}
}
function equipmentInfoChart(){
	equipmentInfoChart = echarts.init(document.getElementById("stationEcharts"));
	equipmentInfoChart.setOption(searchEcharts("vertical", "center",50, 0, "统计信息", [{value: 0, name: "0", "itemStyle": echartStyle('#EAEAEA', false, false, '#EAEAEA')}]));
}
function searchEquipmentInfo(data) {
	totalPage = data && data.totalPage || 0;
	totalCnt = data && data.totalCnt || 0;
	equipmentInfos = data && data.equipmentList || [];
	var equipmentInfoCount = data && data.equipmentListCount || [];
	var length = equipmentInfos.length;
	for (var i = 0;i < length; i++){
		equipmentInfos[i].chargingStationName = '<a class="details" onclick="chargingStationName(' + i + ')">'+equipmentInfos[i].chargingStationName+'</a>'; 
	}
	createTableBody("equipmentInfoTbody", equipmentInfoTable, equipmentInfos);
	var countData = [];
	var	xOne = 25 + (3 - (data && data.activeCount || 0).toString().length) *7;
	var xTwo = 30 - ((data && data.notActiveCount || 0).toString().length - 1) *7;
	$("#svg").html('');
	$("#monitorings").html('');
	$("#noMonitoring").html('');
	$("#svg").html(
		 '<svg style="position: absolute;width: 100px;">' +
            '<circle cx="50" cy="70" r="50" style="fill:#5EE19F;" stroke="white"></circle>' +
            '<text x="' + xOne + '" y="80" font-size="30" style="fill:white">' + (data && data.activeCount || 0) + '</text>' +
         '</svg>' +
         '<svg style="width:80px;margin-left:35%;">' +
             '<circle cx="40" cy="70" r="30" style="fill:#58BCE0;" stroke="white"></circle>' +
             '<text x="' + xTwo + '" y="80" font-size="24" style="fill:white">' + (data && data.notActiveCount || 0) + '</text>' +
     	'</svg>'
			);
	$("#monitorings").html(data && data.activeCount || 0);
	$("#noMonitoring").html(data && data.notActiveCount || 0);
	
	countData.push({"value": (data && data.powerTypeOne || 0), 
		"name":'450KW:'+ (data && data.powerTypeOne || 0), 
		"itemStyle": echartStyle('#58BCE0', true, true,'#58BCE0')
		});
	countData.push({"value": (data && data.powerTypeOTwo || 0), 
		"name":'180KW:' + (data && data.powerTypeOTwo || 0), 
		"itemStyle": echartStyle('#F1AC59', true, true,'#F1AC59')
		});
	option.series[0].data = countData;
	option.legend.data = ['450KW:' + (data && data.powerTypeOne || 0),'180KW:' + (data && data.powerTypeOTwo || 0)];
	equipmentInfoChart.setOption(option, true);
}
function chargingStationName(num){
	window.parent.detalisDrop('chargingStation/chargingStationDetail', num, $("#currentPage").val(), "drownDown1", "" ,"chargingStationPage" ,"chargingStation");
	$("#parentSkipMenu", parent.document).val("chargingStationPage");
	$("#detailID", parent.document).val(equipmentInfos[num].chargingStationID);
}
function search(){
	$ajax('searchEquipmentInfo', 'equipmentSearch', searchEquipmentInfo);
}
function resetSearch(){
	setTimeout(search, 500);
}
function details(index){
	window.parent.detalisSkip('chargingEquipment/equipmentInfoDetail',index,$("#currentPage").val());
	$("#parentSkipMenu", parent.document).val("equipmentPage");
	$("#detailID", parent.document).val(equipmentInfos[index].equipmentID);
}
function chkBefore(index, chkFlag){
	var checkbox =document.getElementsByName("chk"); 
	for(var i=0; i<checkbox.length; i++){
		checkbox[i].checked = false; 
	}
	checkbox[index].checked = true; 
	if(chkFlag == "update"){
		update(index);
	}else if(chkFlag == "delete"){
		del();
	}
}
//添加
function save() {
	window.parent.skipSecondMenu('chargingEquipment/equipmentInfoSave', 'equipmentInfo', 'equipmentPage');
}
//修改
function update(index) {
	window.parent.detalisSkip('chargingEquipment/equipmentInfoUpdate', index, $("#currentPage").val());
	$("#parentSkipMenu", parent.document).val("equipmentPage");
	$("#detailID", parent.document).val(equipmentInfos[index].equipmentID);
}
function confirm(){
	return checkForm("saveForm", equipmentInfoTable);
}
function getUrl(){
	return 'deleteEquipmentInfo';
}
function getData(){
	return {equipmentID: getCheckDataList(equipmentInfos, "equipmentID")};
}
function sure(data){
    var result = data && data.result;
    if (result == "Success") {
		showMessge(messageContent["Delete.Success"]);
	} else if (result == "NotFound") {
		showMessge(messageContent["Delete.NotFound"]);
	} else {
		showMessge(messageContent["Delete.Error"]);
	}
    $ajax('searchEquipmentInfo', 'equipmentSearch', searchEquipmentInfo);
}
